/*
 *  Copyright (C) 2011 Ryan Sullivan.  Contact: ryan@kayoticlabs.com
 * 
 *  This file is part of kayotic-vkm.
 * 
 *  kayotic-vkm is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  kayotic-vkm is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with kayotic-vkm.  If not, see <http://www.gnu.org/licenses/>.
 */

package server;

import java.awt.Point;
import java.awt.Robot;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;


/**
 * This class is the server for receiving commands from the client.
 * It is based on TCP technology, and will be replaced by UDP technology if
 * needed.
 * @author Ryan Sullivan
 */
public class CommandServer {

    private boolean serverOn;
    private int socketNum, respondSocketNum;
    private String clientIP;
    private MouseRobot mouse;
    private Point mousePos;


    public CommandServer() throws Exception{
        socketNum = 6789; //for now ultimately set by startup program
        respondSocketNum = 6791;
        mouse = new MouseRobot();
        mousePos = new Point(0,0);
    }

    public void StartServer() throws Exception{
        serverOn = true;
        /* Start Server */
        ServerSocket welcomeSocket = new ServerSocket(socketNum);
        while(serverOn){
            /* Open Socket and Create Input Stream */
            Socket connectionSocket = welcomeSocket.accept();
            clientIP = connectionSocket.getInetAddress().getHostAddress();
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

            /* Receive Data From Client & Parse */
            String[] message = inFromClient.readLine().split(":");

            /*****************
             Implement Better
             *****************/
            /* Figure out what kind of command it is */
            System.out.println("Get Packet");
            switch(Integer.parseInt(message[0])){

                /* Server Command */
                case 0:
                    System.out.println("System Packet");
                    if(message[1].equals("SetClientServerSocket")){
                        respondSocketNum = Integer.parseInt(message[2]);
                        respondToClient("ack");
                        System.out.println("Client Server Socket:"+message[2]);
                    }
                    else if(message[1].equals("KillServer"))
                    {
                        StopServer();
                    }else{
                        System.out.println(message[1]);
                    }
                    break;
                /* mousecommand */
                case 1:
                    String[] pos = message[1].split(",");
                    int x = mousePos.x + Integer.parseInt(pos[0]);
                    int y = mousePos.y + Integer.parseInt(pos[1]);
                    System.out.println("Mouse Movement:" + x + "," + y);
                    mouse.mousePosition(x, y);
                    break;
            }
        }
    }
    
    private void respondToClient(String message) throws Exception{
        if(respondSocketNum != -1){
            Socket respondSocket = new Socket(clientIP, respondSocketNum);
            DataOutputStream outToClient = new DataOutputStream(respondSocket.getOutputStream());
            outToClient.writeBytes(message);
            outToClient.close();
        }
        else{
            System.out.println("Error: No Client Socket Number");
        }
    }

    public void StopServer(){
        serverOn = false;
    }       
} 